//
// Created by Otowa_kotori on 2016/9/25.
//

#include "entitymanager.h"
#include <cassert>
#include "Entities/actor.h"

EntityManager::EntityManager() {
    this->g = nullptr;
    this->clear();
}
EntityManager::EntityManager(Game *g) {
    this->g = g;
    this->clear();
}

void EntityManager::clear() {
    for (auto i:entityList){
        delete i.second;
    }
    count = 0;
    entityList.clear();
}

// return nullptr on failure
Entity *EntityManager::findEntityByID(const std::string &ID) {
    if (entityList.find(ID) == entityList.end())
        return nullptr;
    if (entityList[ID]->properties["dead"] == "true")
        return nullptr;
    return entityList[ID];
}

void EntityManager::setPos(Actor* actor, Point p){
	// printf("setPos %x\n", g);
	p.z = 0;
    Point oldPos = actor->pos();
    if(actorMap[oldPos] == actor)
		actorMap[oldPos] = nullptr;
    actorMap[p] = actor;
}

// return nullptr on failure
Actor* EntityManager::findActorByPos(Point p) {
	p.z = 0;
    if (actorMap.find(p) != actorMap.end() )
        return actorMap[p];
    return nullptr;
}

EntityManager::~EntityManager() {
    for (auto i: entityList){
        delete i.second;
    }
}
